package com.nsgf.library.data.bean;

import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class Shop {
    private List<ValidListBean> validList;

    public List<ValidListBean> getValidList() {
        return validList;
    }

    public void setValidList(List<ValidListBean> validList) {
        this.validList = validList;
    }

    public static class ValidListBean implements Parcelable {
        /**
         * tenantName : 惠通定制
         * checkAll : false
         * cartId : null
         * specifications : [{"id":244,"selected":false,"spuName":"定制POLO衫","count":2,"property":"颜色:黄色 尺寸:XL","price":6800,"picUrl":"https://zhonghehuitong.com.cn/zhht/35b476a7325fddfa49f366c80c3b49e1677ff1798c9041140e0cdd79816f7ab5.png","spu":{"id":779,"name":"定制POLO衫","picUrl":"https://zhonghehuitong.com.cn/zhht/c04dc9649f6a03e58b24b9638836c91615bde9c8d82536ccde256316599419b6.png","categoryId":19,"tenantName":"惠通定制"},"sku":{"id":208,"picUrl":"https://zhonghehuitong.com.cn/zhht/35b476a7325fddfa49f366c80c3b49e1677ff1798c9041140e0cdd79816f7ab5.png","price":6800,"stock":996,"properties":[{"propertyId":1,"propertyName":"颜色","valueId":24,"valueName":"黄色"},{"propertyId":2,"propertyName":"尺寸","valueId":43,"valueName":"XL"}]}},{"id":243,"selected":false,"spuName":"（包邮）洗护套组(洗发水+护发素）","count":1,"property":"默认:默认","price":22800,"picUrl":"","spu":{"id":763,"name":"（包邮）洗护套组(洗发水+护发素）","picUrl":"https://zhonghehuitong.com.cn/zhht/f5426e2d4ac1ea1fecbf25c8c8d52477f3200ef772426ea0c3e19c96cb43073f.png","categoryId":50,"tenantName":"惠通定制"},"sku":{"id":182,"picUrl":"","price":22800,"stock":999,"properties":[{"propertyId":0,"propertyName":"默认","valueId":0,"valueName":"默认"}]}},{"id":238,"selected":false,"spuName":"定制POLO衫","count":2,"property":"颜色:蓝色 尺寸:4XL","price":6800,"picUrl":"https://zhonghehuitong.com.cn/zhht/bbcf66d370f60c21699a2fcee6a547002a0ed928a9c06d1d8403f501ea101910.png","spu":{"id":779,"name":"定制POLO衫","picUrl":"https://zhonghehuitong.com.cn/zhht/c04dc9649f6a03e58b24b9638836c91615bde9c8d82536ccde256316599419b6.png","categoryId":19,"tenantName":"惠通定制"},"sku":{"id":224,"picUrl":"https://zhonghehuitong.com.cn/zhht/bbcf66d370f60c21699a2fcee6a547002a0ed928a9c06d1d8403f501ea101910.png","price":6800,"stock":995,"properties":[{"propertyId":1,"propertyName":"颜色","valueId":6,"valueName":"蓝色"},{"propertyId":2,"propertyName":"尺寸","valueId":46,"valueName":"4XL"}]}},{"id":237,"selected":false,"spuName":"（包邮）新品福利 小熊猫照射器","count":1,"property":"默认:默认","price":200000,"picUrl":"https://zhonghehuitong.com.cn/zhht/766246cddace8379b405ed65c039c4bd98a31ed07760116eb94785d87b3593ef.jpg","spu":{"id":674,"name":"（包邮）新品福利 小熊猫照射器","picUrl":"https://zhonghehuitong.com.cn/zhht/766246cddace8379b405ed65c039c4bd98a31ed07760116eb94785d87b3593ef.jpg","categoryId":32,"tenantName":"惠通定制"},"sku":{"id":93,"picUrl":"https://zhonghehuitong.com.cn/zhht/766246cddace8379b405ed65c039c4bd98a31ed07760116eb94785d87b3593ef.jpg","price":200000,"stock":995,"properties":[{"propertyId":0,"propertyName":"默认","valueId":0,"valueName":"默认"}]}}]
         */

        private String tenantName;
        private boolean checkAll;
        private String cartId;
        private List<SpecificationsBean> specifications;
        private boolean isSelected;

        public ValidListBean(){}

        protected ValidListBean(Parcel in) {
            tenantName = in.readString();
            checkAll = in.readByte() != 0;
            specifications = in.createTypedArrayList(SpecificationsBean.CREATOR);
            isSelected = in.readByte() != 0;
        }

        public static final Creator<ValidListBean> CREATOR = new Creator<ValidListBean>() {
            @Override
            public ValidListBean createFromParcel(Parcel in) {
                return new ValidListBean(in);
            }

            @Override
            public ValidListBean[] newArray(int size) {
                return new ValidListBean[size];
            }
        };

        public int getSelectedCount() {
            int count = 0;
            for (SpecificationsBean item : specifications) {
                if (item.isSelected()) {
                    count++;
                }
            }
            return count;
        }

        public boolean isAllSelected() {
            if (specifications.isEmpty()) return false;
            for (SpecificationsBean item : specifications) {
                if (!item.isSelected()) {
                    return false;
                }
            }
            return true;
        }

        public void updateShopSelection() {
            boolean newSelection = isAllSelected();
            if (this.isSelected != newSelection) {
                this.isSelected = newSelection;
            }
        }

        public void selectAllItems(boolean selected) {
            for (SpecificationsBean item : specifications) {
                item.setSelected(selected);
            }
            this.isSelected = selected;
        }
        public double getTotalPrice() {
            double total = 0;
            for (SpecificationsBean item : specifications) {
                if (item.isSelected()) {
                    total += item.getTotalPrice();
                }
            }
            return total;
        }
        public String getTenantName() {
            return tenantName;
        }

        public void setTenantName(String tenantName) {
            this.tenantName = tenantName;
        }

        public boolean isCheckAll() {
            return checkAll;
        }

        public void setCheckAll(boolean checkAll) {
            this.checkAll = checkAll;
        }

        public String getCartId() {
            return cartId;
        }

        public void setCartId(String cartId) {
            this.cartId = cartId;
        }

        public List<SpecificationsBean> getSpecifications() {
            if (null==specifications)
                specifications = new ArrayList<>();
            return specifications;
        }

        public void setSpecifications(List<SpecificationsBean> specifications) {
            this.specifications = specifications;
        }

        public boolean isSelected() {
            return isSelected;
        }

        public void setSelected(boolean selected) {
            isSelected = selected;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(@NonNull Parcel dest, int flags) {
            dest.writeString(tenantName);
            dest.writeByte((byte) (checkAll ? 1 : 0));
            dest.writeTypedList(specifications);
            dest.writeByte((byte) (isSelected ? 1 : 0));
        }


        public static class SpecificationsBean implements Parcelable {

            private String id;
            private boolean selected;
            private String spuName;
            private int count;
            private String property;
            private double price;
            private String picUrl;
            private SpuBean spu;
            private SkuBean sku;


            public SpecificationsBean(){}
            protected SpecificationsBean(Parcel in) {
                id = in.readString();
                selected = in.readByte() != 0;
                spuName = in.readString();
                count = in.readInt();
                property = in.readString();
                price = in.readDouble();
                picUrl = in.readString();
                spu = in.readParcelable(SpuBean.class.getClassLoader());
                sku = in.readParcelable(SkuBean.class.getClassLoader());
            }

            public static final Creator<SpecificationsBean> CREATOR = new Creator<SpecificationsBean>() {
                @Override
                public SpecificationsBean createFromParcel(Parcel in) {
                    return new SpecificationsBean(in);
                }

                @Override
                public SpecificationsBean[] newArray(int size) {
                    return new SpecificationsBean[size];
                }
            };

            public double getTotalPrice() {
                return price/100 * count;
            }
            public String getId() {
                return id;
            }

            public void setId(String id) {
                this.id = id;
            }

            public boolean isSelected() {
                return selected;
            }

            public void setSelected(boolean selected) {
                this.selected = selected;
            }

            public String getSpuName() {
                return spuName;
            }

            public void setSpuName(String spuName) {
                this.spuName = spuName;
            }

            public int getCount() {
                return count;
            }
            public String getCountSrc() {
                return "x"+count;
            }

            public void setCount(int count) {
                this.count = count;
            }

            public String getProperty() {
                return property;
            }

            public void setProperty(String property) {
                this.property = property;
            }

            public double getPrice() {
                return price;
            }
            public String getPriceSrc() {
                DecimalFormat df = new DecimalFormat("#####0.00");
                return "¥"+df.format(price/100);
            }

            public void setPrice(double price) {
                this.price = price;
            }

            public String getPicUrl() {
                return picUrl;
            }

            public void setPicUrl(String picUrl) {
                this.picUrl = picUrl;
            }

            public SpuBean getSpu() {
                return spu;
            }

            public void setSpu(SpuBean spu) {
                this.spu = spu;
            }

            public SkuBean getSku() {
                return sku;
            }

            public void setSku(SkuBean sku) {
                this.sku = sku;
            }

            @Override
            public int describeContents() {
                return 0;
            }

            @Override
            public void writeToParcel(@NonNull Parcel dest, int flags) {
                dest.writeString(id);
                dest.writeByte((byte) (selected ? 1 : 0));
                dest.writeString(spuName);
                dest.writeInt(count);
                dest.writeString(property);
                dest.writeDouble(price);
                dest.writeString(picUrl);
                dest.writeParcelable(spu, flags);
                dest.writeParcelable(sku, flags);
            }


            public static class SpuBean implements Parcelable{
                /**
                 * id : 779
                 * name : 定制POLO衫
                 * picUrl : https://zhonghehuitong.com.cn/zhht/c04dc9649f6a03e58b24b9638836c91615bde9c8d82536ccde256316599419b6.png
                 * categoryId : 19
                 * tenantName : 惠通定制
                 */

                private int id;
                private String name;
                private String picUrl;
                private int categoryId;
                private String tenantName;

                protected SpuBean(Parcel in) {
                    id = in.readInt();
                    name = in.readString();
                    picUrl = in.readString();
                    categoryId = in.readInt();
                    tenantName = in.readString();
                }

                public static final Creator<SpuBean> CREATOR = new Creator<SpuBean>() {
                    @Override
                    public SpuBean createFromParcel(Parcel in) {
                        return new SpuBean(in);
                    }

                    @Override
                    public SpuBean[] newArray(int size) {
                        return new SpuBean[size];
                    }
                };

                public int getId() {
                    return id;
                }

                public void setId(int id) {
                    this.id = id;
                }

                public String getName() {
                    return name;
                }

                public void setName(String name) {
                    this.name = name;
                }

                public String getPicUrl() {
                    return picUrl;
                }

                public void setPicUrl(String picUrl) {
                    this.picUrl = picUrl;
                }

                public int getCategoryId() {
                    return categoryId;
                }

                public void setCategoryId(int categoryId) {
                    this.categoryId = categoryId;
                }

                public String getTenantName() {
                    return tenantName;
                }

                public void setTenantName(String tenantName) {
                    this.tenantName = tenantName;
                }

                @Override
                public int describeContents() {
                    return 0;
                }

                @Override
                public void writeToParcel(@NonNull Parcel dest, int flags) {
                    dest.writeInt(id);
                    dest.writeString(name);
                    dest.writeString(picUrl);
                    dest.writeInt(categoryId);
                    dest.writeString(tenantName);
                }
            }

            public static class SkuBean implements Parcelable{
                /**
                 * id : 208
                 * picUrl : https://zhonghehuitong.com.cn/zhht/35b476a7325fddfa49f366c80c3b49e1677ff1798c9041140e0cdd79816f7ab5.png
                 * price : 6800
                 * stock : 996
                 * properties : [{"propertyId":1,"propertyName":"颜色","valueId":24,"valueName":"黄色"},{"propertyId":2,"propertyName":"尺寸","valueId":43,"valueName":"XL"}]
                 */

                private String id;
                private String picUrl;
                private double price;
                private int stock;
                private List<PropertiesBean> properties;


                public SkuBean(){}
                protected SkuBean(Parcel in) {
                    id = in.readString();
                    picUrl = in.readString();
                    price = in.readDouble();
                    stock = in.readInt();
                    properties = in.createTypedArrayList(PropertiesBean.CREATOR);
                }

                public static final Creator<SkuBean> CREATOR = new Creator<SkuBean>() {
                    @Override
                    public SkuBean createFromParcel(Parcel in) {
                        return new SkuBean(in);
                    }

                    @Override
                    public SkuBean[] newArray(int size) {
                        return new SkuBean[size];
                    }
                };

                public String getId() {
                    return id;
                }

                public void setId(String id) {
                    this.id = id;
                }

                public String getPicUrl() {
                    return picUrl;
                }

                public void setPicUrl(String picUrl) {
                    this.picUrl = picUrl;
                }

                public double getPrice() {
                    return price;
                }

                public void setPrice(double price) {
                    this.price = price;
                }

                public int getStock() {
                    return stock;
                }

                public void setStock(int stock) {
                    this.stock = stock;
                }

                public List<PropertiesBean> getProperties() {
                    return properties;
                }

                public void setProperties(List<PropertiesBean> properties) {
                    this.properties = properties;
                }

                @Override
                public int describeContents() {
                    return 0;
                }

                @Override
                public void writeToParcel(@NonNull Parcel dest, int flags) {
                    dest.writeString(id);
                    dest.writeString(picUrl);
                    dest.writeDouble(price);
                    dest.writeInt(stock);
                    dest.writeTypedList(properties);
                }

                public static class PropertiesBean implements Parcelable{
                    /**
                     * propertyId : 1
                     * propertyName : 颜色
                     * valueId : 24
                     * valueName : 黄色
                     */

                    private int propertyId;
                    private String propertyName;
                    private int valueId;
                    private String valueName;

                    protected PropertiesBean(Parcel in) {
                        propertyId = in.readInt();
                        propertyName = in.readString();
                        valueId = in.readInt();
                        valueName = in.readString();
                    }

                    public static final Creator<PropertiesBean> CREATOR = new Creator<PropertiesBean>() {
                        @Override
                        public PropertiesBean createFromParcel(Parcel in) {
                            return new PropertiesBean(in);
                        }

                        @Override
                        public PropertiesBean[] newArray(int size) {
                            return new PropertiesBean[size];
                        }
                    };

                    public int getPropertyId() {
                        return propertyId;
                    }

                    public void setPropertyId(int propertyId) {
                        this.propertyId = propertyId;
                    }

                    public String getPropertyName() {
                        return propertyName;
                    }

                    public void setPropertyName(String propertyName) {
                        this.propertyName = propertyName;
                    }

                    public int getValueId() {
                        return valueId;
                    }

                    public void setValueId(int valueId) {
                        this.valueId = valueId;
                    }

                    public String getValueName() {
                        return valueName;
                    }

                    public void setValueName(String valueName) {
                        this.valueName = valueName;
                    }

                    @Override
                    public int describeContents() {
                        return 0;
                    }

                    @Override
                    public void writeToParcel(@NonNull Parcel dest, int flags) {
                        dest.writeInt(propertyId);
                        dest.writeString(propertyName);
                        dest.writeInt(valueId);
                        dest.writeString(valueName);
                    }
                }
            }
        }
    }
}